#include "TaskQueue.h"

TaskQueue::TaskQueue(size_t capa)
:_capacity(capa)
,_que()
,_mutex()
,_notEmpty()
,_notFull()
,_flag(true)
{}

TaskQueue::~TaskQueue(){

};

void TaskQueue::push(ElemType value){
    unique_lock<mutex> ul(_mutex);
    while(full()){
        _notFull.wait(ul);
    }
    _que.push(value);
    _notEmpty.notify_one();
}
ElemType TaskQueue::pop(){
    unique_lock<mutex> ul(_mutex);
    while(empty() && _flag){
        _notEmpty.wait(ul);
    }

    if(_flag){
        ElemType tmp = _que.front();
        _que.pop();
        _notFull.notify_one();

        return nullptr;
    }else{
        return nullptr;
    }
}

bool TaskQueue::empty() const{
    return _que.size() == 0;
}
bool TaskQueue::full() const{
    return _que.size() == _capacity;
}

void TaskQueue::wakeup(){
    _flag = false;
    _notEmpty.notify_all();
}


